package com.glsc.ngateway.ldap.service;

import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrCompDto;
import com.glsc.ngateway.common.base.converter.ldap.LdapEhrCompConverter;
import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.LdapEhrComp;
import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.LdapEhrEmp;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrCompSearchDto;
import com.glsc.ngateway.common.base.repo.mysql.gateway.ldap.LdapEhrCompRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.ldap.LdapEhrEmpRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * @author xuchang
 */
@Service
public class LdapEhrCompService {

    Logger logger = LoggerFactory.getLogger(LdapEhrCompService.class);

    @Resource
    private LdapEhrCompRepo ldapEhrCompRepo;

    @Resource
    private LdapEhrEmpRepo ldapEhrEmpRepo;

    public List<LdapEhrCompDto> findAll(LdapEhrCompSearchDto param) {
        return LdapEhrCompConverter.domain2Dto(ldapEhrCompRepo.query(param));
    }

    public LdapEhrComp findByEmpAccount(@NotBlank String account){
        LdapEhrEmp byBadge = ldapEhrEmpRepo.findByBadge(account);
        if(Objects.nonNull(byBadge) && Objects.nonNull(byBadge.getOaCompId())){
            List<LdapEhrComp> list = ldapEhrCompRepo.findAllByOaCompId(byBadge.getOaCompId());
            return CollectionUtil.isNotEmpty(list) ? list.get(0) : null;
        }
        return null;
    }
}
